package com.example.anpnovel.controller.crud;

import com.example.anpapi.entity.po.novelmodel.Comment.Comment;
import com.example.anpapi.entity.vo.R;
import com.example.anpnovel.service.interfaces.CommentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    /**
     * 评论添加
     * @param comment
     * @return
     */
    @PostMapping("/save")
    public R<Comment> save(@RequestBody Comment comment){
        log.info("评论添加:{}", comment);
        commentService.save(comment);
        return R.success(comment,"评论添加成功");
    }

    /**
     * 根据评论id删除评论
     * @param comment
     * @return
     */
    @DeleteMapping("/remove")
    public R<String> remove(@RequestBody Comment comment){
        log.info("评论删除:{}", comment);
        if(commentService.getById(comment.getId()) != null)
        {
            commentService.removeById(comment.getId());
            return R.success(null,"评论删除成功");
        }
        return R.error("未找到该评论");
    }

    /**
     * 根据评论id查找评论
     * @param id
     * @return
     */
    @GetMapping("/get")
    public R<Comment> get(@RequestParam(value = "id") Long id) {
        log.info("查询评论的id为{}", id);
        Comment comment = commentService.getById(id);
        return R.success(comment, "查询评论成功");
    }

    /**
     * 根据章节id查找所有评论
     * @param chapterId
     * @return
     */
    @GetMapping("/getByChapterId")
    public R<List<Comment>> getByChapterId(@RequestParam(value = "chapterId") Long chapterId) {
        log.info("查询章节id为{}的所有评论", chapterId);
        List<Comment> comments = commentService.lambdaQuery()
                .eq(Comment::getChapterId, chapterId)
                .isNull(Comment::getParentId)
                .orderByDesc(Comment::getCreatedTime)
                .list();
        return R.success(comments, "查询评论成功");
    }

    /**
     * 根据父评论id查找所有回复
     * @param parentId
     * @return
     */
    @GetMapping("/getByParentId")
    public R<List<Comment>> getByParentId(@RequestParam(value = "parentId") Long parentId) {
        log.info("查询父评论id为{}的所有回复", parentId);
        List<Comment> comments = commentService.lambdaQuery()
                .eq(Comment::getParentId, parentId)
                .orderByAsc(Comment::getCreatedTime)
                .list();
        return R.success(comments, "查询回复成功");
    }

    /**
     * 根据用户id查找所有评论
     * @param userId
     * @return
     */
    @GetMapping("/getByUserId")
    public R<List<Comment>> getByUserId(@RequestParam(value = "userId") Long userId) {
        log.info("查询用户id为{}的所有评论", userId);
        List<Comment> comments = commentService.lambdaQuery()
                .eq(Comment::getUserId, userId)
                .orderByDesc(Comment::getCreatedTime)
                .list();
        return R.success(comments, "查询评论成功");
    }

    /**
     * 修改评论
     * @param comment
     * @return
     */
    @PostMapping("/update")
    public R<Comment> update(@RequestBody Comment comment){
        log.info("修改的评论为{}", comment);
        commentService.updateById(comment);
        return R.success(comment,"修改评论成功");
    }

}

